﻿#if DatabaseTests
using System;
using System.Linq;
using NUnit.Framework;
#if DatabaseTests_NHibernate
using NHibernate;
using OpenWaves.ActivityFeed.NHibernate;
#endif

#if DatabaseTests_RavenDB
using OpenWaves.ActivityFeed.RavenDB;

#endif

namespace OpenWaves.ActivityFeed.UnitTests
{
    public abstract class ActivityFeedServiceRolesTest : ActivityFeedServiceTestsBase
    {
        private void createUpdates(IActivityFeedService service)
        {
            using (var fakeTime = FakeTimeScope.Begin())
            {
                fakeTime.Now = new DateTime(2012, 01, 01);
                service.PublishUpdate(new Post("Public Update 1"), "user1111", "source", new Topic[] { Topic.FromUserId("user1111") }, true, Enumerable.Empty<UserGroup>());
                fakeTime.Now = new DateTime(2012, 01, 02);
                service.PublishUpdate(new Post("Public Update 2"), "user1111", "source", new Topic[] { Topic.FromUserId("user1111") }, true, new[] { new UserGroup("dumyRole") });
                fakeTime.Now = new DateTime(2012, 01, 03);
                service.PublishUpdate(new Post("Admin Update"), "user1111", "source", new Topic[] { Topic.FromUserId("user1111") }, false, new[] { new UserGroup("admin") });
                fakeTime.Now = new DateTime(2012, 01, 04);
                service.PublishUpdate(new Post("Manager Update"), "user1111", "source", new Topic[] { Topic.FromUserId("user1111") }, false, new[] { new UserGroup("manager") });
                fakeTime.Now = new DateTime(2012, 01, 05);
                service.PublishUpdate(new Post("Manager Update 2"), "user1111", "source", new Topic[] { Topic.FromUserId("user1111") }, false, new[] { new UserGroup("manager") });
                fakeTime.Now = new DateTime(2012, 01, 06);
                service.PublishUpdate(new Post("Employee Update"), "user1111", "source", new Topic[] { Topic.FromUserId("user1111") }, false, new[] { new UserGroup("employee") });
            }
        }

        [Test]
        public void Given_test_updates_When_user_do_not_have_required_roles_Then_GetUpdates_returns_public_documents_only()
        {
            Given(createUpdates);

            var userGroups = new [] { new UserGroup("dummyRoleUser"), new UserGroup("dummyRoleUser2") };
           
            Then(service =>
            {
                var updates = service.GetUpdates().SecurityTrim(userGroups).ToList();

                Assert.That(updates.Count(), Is.EqualTo(2));
                Assert.That(updates[0].ContentText(), Is.EqualTo("Public Update 2"));
                Assert.That(updates[1].ContentText(), Is.EqualTo("Public Update 1"));
            });
        }

        [Test]
        public void Given_test_updates_When_user_have_required_roles_Then_GetUpdates_returns_public_and_granted_documents()
        {
            Given(createUpdates);

            var userGroups = new [] { new UserGroup("manager"), new UserGroup("employee"), new UserGroup("dummyRole") };

            Then(service =>
            {
                var updates = service.GetUpdates().SecurityTrim(userGroups).ToList();

                Assert.That(updates.Count(), Is.EqualTo(5));
                Assert.That(updates[0].ContentText(), Is.EqualTo("Employee Update"));
                Assert.That(updates[1].ContentText(), Is.EqualTo("Manager Update 2"));
                Assert.That(updates[2].ContentText(), Is.EqualTo("Manager Update"));
                Assert.That(updates[3].ContentText(), Is.EqualTo("Public Update 2"));
                Assert.That(updates[4].ContentText(), Is.EqualTo("Public Update 1"));
            });
        }
    }
}
#endif